<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>VK Memory</title>


        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
        <script src="http://vkontakte.ru/js/api/openapi.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(function(){
                VK.init({
                    apiId: 2287579
                });

                $('a#vk-login').click(function(event){
                    event.preventDefault();
                    VK.Auth.login(null, VK.access.FRIENDS);
                    return false;
                });

                $('a#vk-logout').click(function(event){
                    event.preventDefault();
                    VK.Auth.logout(function(){
                        refreshPage(false);
                    });
                });

//                VK.Observer.subscribe('auth.login', function(response){
//                    refreshPage(true);
//                });
                VK.Observer.subscribe('auth.sessionChange', function(response){
                    refreshPage(true);
                });

            });

            var isLogged = false;
            function refreshPage(checkLogged)
            {
                if((checkLogged && !isLogged) || !checkLogged)
                {
                    isLogged = true;
                    location.href = 'index.php';
                }
            }

            VK.Auth.getLoginStatus(function(response){
                if(response.session)
                {
                    alert("Is auth");
                    VK.Api.call('friends.get', {
                        fields: 'photo_rec'
                    }, function(data){
                        if(!data.error)
                        {
                            if(data.response.length > 0)
                            {
                                data.response.shuffle();
                                for(i = 0; i < data.response.length; i++)
                                {
                                    var friend = data.response[i];
                                    if(friend.photo_rec && friend.photo_rec.indexOf('images/question_c.gif') == -1)
                                    {
                                        photos.push({
                                            photo: friend.photo_rec,
                                            id: friend.uid
                                        });
                                    }
                                }
                            }
                            initGameField();
                        }
                        else
                        {
                            if(data.error.error_code == 7)
                            {
                                isLogged = false;
                                $('div#login-bar').show();
                                $('div#logout-bar').hide();
                                $.unblockUI();
                            }
                            else
                            {
                                showError(data.error.error_msg);
                            }
                        }
                    });
                }
                else
                {
                    // пользователь не авторизирован
                }
            });
        </script>

    </head>

    <body>

        <center>

            <div style="text-align: center;">
                <a href="#" id="vk-login">Авторизуватись</a>
            </div>
            <div style="text-align: center;">
                <a href="#" id="vk-logout">Вийти</a>
            </div>

        </center>

    </body>

</html>